package com.archermind.style.controller;

import com.archermind.style.bean.*;
import com.archermind.style.pojo.CombineBean;
import com.archermind.style.pojo.ElementEnum;
import com.archermind.style.pojo.GroupEditBean;
import com.archermind.style.pojo.RuleText;
import com.archermind.style.service.InstanceService;
import com.archermind.style.service.OperationService;
import com.archermind.style.service.RuleService;
import com.archermind.style.util.Result;
import com.archermind.style.util.Search;
import com.feng.fengDUtil.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "/rule")
public class RuleAction {

    @Autowired
    private RuleService ruleService;

    @Autowired
    private InstanceService instanceService;
    @Autowired
    private OperationService opService;

    @RequestMapping(value = "/list")
    public Result listIconType( Page page,Search search) {
        ruleService.query(page,search);
        return Result.success(page);
    }


    @RequestMapping(value="/saveRulePoint")
    public Result saveRulePoint(@RequestBody RulePoint rulePoint){
        try {
            opService.saveLog((rulePoint.getId()==null?"添加":"修改")+"了点样式",rulePoint.getInstanceId());
            StyleType textStyleType = ruleService.saveRulePoint(rulePoint);
            Map map = new HashMap<>();
            map.put("rule",rulePoint);
            map.put("textStyleType",textStyleType);
            return Result.success(map);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @RequestMapping(value="/saveRuleLine")
    public Result saveRuleLine(@RequestBody RuleLine ruleLine){
        try {
            opService.saveLog((ruleLine.getId()==null?"添加":"修改")+"了线样式",ruleLine.getInstanceId());
            StyleType textStyleType = ruleService.saveRuleLine(ruleLine);
            Map map = new HashMap<>();
            map.put("rule",ruleLine);
            map.put("textStyleType",textStyleType);
            return Result.success(map);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @RequestMapping(value="/saveRulePolygon")
    public Result saveRulePolygon(@RequestBody RulePolygon rulePolygon){
        try {
            opService.saveLog((rulePolygon.getId()==null?"添加":"修改")+"了面样式",rulePolygon.getInstanceId());
            ruleService.saveOrUpdate(rulePolygon);
            Map map = new HashMap<>();
            map.put("rule",rulePolygon);
            return Result.success(map);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @RequestMapping(value="/saveRuleRoadSign")
    public Result saveRuleRoadSign(@RequestBody RuleRoadSign ruleRoadSign){
        try {
            opService.saveLog((ruleRoadSign.getId()==null?"添加":"修改")+"了路牌样式",ruleRoadSign.getInstanceId());
            StyleType textStyleType = ruleService.saveRuleRoadSign(ruleRoadSign);
            Map map = new HashMap<>();
            map.put("rule",ruleRoadSign);
            map.put("textStyleType",textStyleType);
            return Result.success(map);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @RequestMapping(value="/saveRuleBuilding")
    public Result saveRuleBuilding(@RequestBody RuleBuilding ruleBuilding){
        try {
            opService.saveLog((ruleBuilding.getId()==null?"添加":"修改")+"了建筑样式",ruleBuilding.getInstanceId());
            ruleService.saveOrUpdate(ruleBuilding);
            Map map = new HashMap<>();
            map.put("rule",ruleBuilding);
            return Result.success(map);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @RequestMapping(value="/saveRuleArrow")
    public Result saveRuleArrow(@RequestBody RuleArrow ruleArrow){
        try {
            opService.saveLog((ruleArrow.getId()==null?"添加":"修改")+"了箭头样式",ruleArrow.getInstanceId());
            ruleService.saveOrUpdate(ruleArrow);
            Map map = new HashMap<>();
            map.put("rule",ruleArrow);
            return Result.success(map);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @RequestMapping(value="/saveRuleText")
    public Result saveRuleText(@RequestBody RuleText ruleText){
        try {
            ruleService.saveOrUpdate(ruleText);
            return Result.success(ruleText);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @RequestMapping(value="/del")
    public Result del(String ids,ElementEnum element){
        try {
            opService.saveLog("删除了"+element.getName()+"样式");
            ruleService.delRule(ids,element);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }




    @RequestMapping(value="/combine")
    public Result combine(@RequestBody CombineBean combinBean){
        try {
            ruleService.combine(combinBean);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @RequestMapping(value="/groupEdit")
    public Result groupEdit(@RequestBody GroupEditBean groupEditBean){
        try {
            ruleService.groupEdit(groupEditBean);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @RequestMapping(value="/copyStyle")
    public Result copyStyle(@RequestParam(required = true) Integer instanceId,
                            @RequestParam(required = true) Integer styleId,
                            @RequestParam(required = true) ElementEnum element,
                            @RequestParam(required = true) Integer oldInstanceId,
                            @RequestParam(required = true) Integer[] ruleIds){
        try {
            ruleService.copyStyle(instanceId, styleId, element, oldInstanceId, ruleIds);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }



}